<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="职工姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入职工姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="线路名称" prop="routeId">
        <el-select v-model="queryParams.routeId" filterable placeholder="请选择路线" clearable size="small">
          <el-option
            v-for="route in routeList"
            :key="route.routeId"
            :label="route.routeName"
            :value="route.routeId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="预约状态" prop="appointStatus">
      <el-select v-model="queryParams.appointStatus" placeholder="请选择预约状态" clearable size="small">
        <el-option
          v-for="dict in appointStatusOptions"
          :key="dict.dictValue"
          :label="dict.dictLabel"
          :value="dict.dictValue"
        />
      </el-select>
    </el-form-item>
      <el-form-item label="班次" prop="driveTime">
        <el-select v-model="queryParams.driveTime" placeholder="请选择预约班次" clearable size="small">
          <el-option
            v-for="dict in driveTimeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发车时间" prop="gotoTime">
        <el-date-picker
          clearable size="small"
          v-model="queryParams.gotoTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择发车时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5" v-if="false">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['bus:appoint:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5" v-if="false">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['bus:appoint:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5" v-if="false">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['bus:appoint:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['bus:appoint:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="appointList" @selection-change="handleSelectionChange">
      <!--<el-table-column type="selection" width="55" align="center" />-->
      <!--<el-table-column label="预约ID" align="center" prop="appointId" />-->
      <el-table-column label="预约人" align="center" prop="userName" sortable />
      <el-table-column label="预约线路" align="center" prop="routeName" sortable />
      <!--<el-table-column label="预约路线ID" align="center" prop="routeId" />-->
      <el-table-column label="起始站" align="center" prop="startSite" sortable />
      <el-table-column label="终点站" align="center" prop="endSite" sortable />
      <!--<el-table-column label="上车站点ID" align="center" prop="siteId" />-->
      <el-table-column label="用户预约站点" align="center" prop="siteName" sortable />
      <el-table-column label="发车日期" align="center" prop="gotoTime" sortable />
      <el-table-column label="预约班次" align="center" prop="driveTime" :formatter="driveTimeFormat" sortable />
      <!--<el-table-column label="预约人ID" align="center" prop="userId" />-->
      <el-table-column label="司机" align="center" prop="driverUserName" sortable />
      <!--<el-table-column label="车辆ID" align="center" prop="busId" />-->
      <el-table-column label="车牌号" align="center" prop="busNumber" sortable />
      <el-table-column label="车辆型号" align="center" prop="busModel" sortable />
      <!--<el-table-column label="司机ID" align="center" prop="driverUserId" />-->
      <el-table-column label="预约状态" align="center" prop="appointStatus" :formatter="appointStatusFormat" sortable />
      <el-table-column label="预约时间" align="center" prop="createTime" sortable />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width"  v-if="false">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['bus:appoint:edit']"
            v-if="false"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['bus:appoint:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车辆预约对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="预约班次" prop="driveTime">
          <el-input v-model="form.driveTime" placeholder="请输入预约班次" />
        </el-form-item>
        <el-form-item label="预约路线ID" prop="routeId">
          <el-input v-model="form.routeId" placeholder="请输入预约路线ID" />
        </el-form-item>
        <el-form-item label="预约线路名称" prop="routeName">
          <el-input v-model="form.routeName" placeholder="请输入预约线路名称" />
        </el-form-item>
        <el-form-item label="起始站" prop="startSite">
          <el-input v-model="form.startSite" placeholder="请输入起始站" />
        </el-form-item>
        <el-form-item label="终点站" prop="endSite">
          <el-input v-model="form.endSite" placeholder="请输入终点站" />
        </el-form-item>
        <el-form-item label="上车站点ID" prop="siteId">
          <el-input v-model="form.siteId" placeholder="请输入上车站点ID" />
        </el-form-item>
        <el-form-item label="站点名称" prop="siteName">
          <el-input v-model="form.siteName" placeholder="请输入站点名称" />
        </el-form-item>
        <el-form-item label="预约人ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入预约人ID" />
        </el-form-item>
        <el-form-item label="预约人名称" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入预约人名称" />
        </el-form-item>
        <el-form-item label="车辆ID" prop="busId">
          <el-input v-model="form.busId" placeholder="请输入车辆ID" />
        </el-form-item>
        <el-form-item label="车牌号" prop="busNumber">
          <el-input v-model="form.busNumber" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="车辆型号" prop="busModel">
          <el-input v-model="form.busModel" placeholder="请输入车辆型号" />
        </el-form-item>
        <el-form-item label="司机ID" prop="driverUserId">
          <el-input v-model="form.driverUserId" placeholder="请输入司机ID" />
        </el-form-item>
        <el-form-item label="司机名称" prop="driverUserName">
          <el-input v-model="form.driverUserName" placeholder="请输入司机名称" />
        </el-form-item>
        <el-form-item label="预约状态">
          <el-radio-group v-model="form.appointStatus">
            <el-radio
              v-for="dict in appointStatusOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAppoint, getAppoint, delAppoint, addAppoint, updateAppoint, exportAppoint } from "@/api/bus/appoint";
import { listRoute } from "@/api/bus/route";

export default {
  name: "Appoint",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆预约表格数据
      appointList: [],
      routeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 预约班次字典
      driveTimeOptions: [],
      // 预约状态字典
      appointStatusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        driveTime: null,
        routeId: null,
        routeName: null,
        gotoTime: null,
        startSite: null,
        endSite: null,
        siteId: null,
        siteName: null,
        userId: null,
        userName: null,
        busId: null,
        busNumber: null,
        busModel: null,
        driverUserId: null,
        driverUserName: null,
        appointStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.listRouteData();
    this.getDicts("booking_frequency").then(response => {
      this.driveTimeOptions = response.data;
    });
    this.getDicts("appoint_status").then(response => {
      this.appointStatusOptions = response.data;
    });
  },
  methods: {
    /** 查询车辆预约列表 */
    getList() {
      this.loading = true;
      listAppoint(this.queryParams).then(response => {
        this.appointList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    listRouteData(){
      let query = {

      }
      listRoute(query).then(res =>{
          this.routeList = res.rows;
      });
    },
    // 预约班次字典翻译
    driveTimeFormat(row, column) {
      return this.selectDictLabel(this.driveTimeOptions, row.driveTime);
    },
    // 预约状态字典翻译
    appointStatusFormat(row, column) {
      return this.selectDictLabel(this.appointStatusOptions, row.appointStatus);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        appointId: null,
        driveTime: null,
        routeId: null,
        routeName: null,
        startSite: null,
        endSite: null,
        siteId: null,
        siteName: null,
        userId: null,
        userName: null,
        busId: null,
        busNumber: null,
        busModel: null,
        driverUserId: null,
        driverUserName: null,
        appointStatus: 0,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.appointId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加车辆预约";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const appointId = row.appointId || this.ids
      getAppoint(appointId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改车辆预约";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.appointId != null) {
            updateAppoint(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAppoint(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const appointIds = row.appointId || this.ids;
      this.$confirm('是否确认删除车辆预约编号为"' + appointIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delAppoint(appointIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有车辆预约数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportAppoint(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
